package YNU.dao.impl;

import YNU.dao.TeacherDao;
import YNU.po.Student;
import YNU.po.Teacher;
import YNU.util.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {

    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public Teacher getTeacherByNameByPass(String cname, String password){

        Teacher teacher=null;
        String sql = "select * from teacher where cname=? and password=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, cname);
            pst.setString(2, password);
            rs = pst.executeQuery();
            while(rs.next()) {
                teacher = new Teacher();
                teacher.setCid(rs.getInt("cid"));
                teacher.setCname(rs.getString("cname"));
                teacher.setPassword(rs.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return teacher;
    }
    public int saveTeacher(String cname ,int password) {
        int sid = 0;
        String sql = "insert into teacher(cname,password) values(?,?)";//改成111
        try {
            con = DBUtil.getConnection();
            //设置返回自增长列值
            pst = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            pst.setString(1, cname);
            pst.setInt(2, password);
            pst.executeUpdate();
            //获取自增长列值（一行一列）
            rs = pst.getGeneratedKeys();
            if (rs.next()) {
                sid = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return sid;
    }

    public List<Teacher> listTeacher(String name, String password){
        List<Teacher> list = new ArrayList<>();
        StringBuffer sql = new StringBuffer("select * from Teacher where 1=1 ");
        if (name != null && !name.equals("")) {
            sql.append(" and name like '%" + name + "%' ");
        }
        if (password != null && !password.equals("")) {
            sql.append(" and password like '%" + password + "%' ");
        }
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql.toString());
            rs = pst.executeQuery();
            while (rs.next()) {
                Teacher teacher = new Teacher();
                teacher.setCid(rs.getInt("cid"));
                teacher.setCname(rs.getString("cname"));
                teacher.setPassword(rs.getString("password"));


                //               teacher.setBusinessExplain(rs.getString("businessExplain"));
//                teacher.setStarPrice(rs.getDouble("starPrice"));
//                teacher.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                list.add(teacher);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pst, con);
        }
        return list;
    }
    public int removeTeacher(int cid){
        int result = 0;
        // String delFootSql = "delete from student where sid=?";
        String delTeacherSql = "delete from Teacher where cid=?";
        try {
            con = DBUtil.getConnection();
            //开启一个事务
            con.setAutoCommit(false);//

            pst = con.prepareStatement(delTeacherSql);
            pst.setInt(1, cid);
            result = pst.executeUpdate();

            con.commit();
        } catch (SQLException e) {
            result = 0;
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close(null, pst, con);
        }
        return result;
    }
}
